package cn.gz.demo.utils;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.viewpager.widget.ViewPager;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.List;

/**
 * @author Liuyz
 * @date 2020/5/2514:10
 */
public class TabUtils {
    static TabUtils instance;

    public static TabUtils getInstance() {
        if (instance == null) {
            instance = new TabUtils();
        }
        return instance;
    }

    /**
     * 默认tab样式
     *
     * @param mContext
     * @param viewpager
     * @param titles
     * @return
     */
    public CommonNavigator initNormalTab(Context mContext, ViewPager viewpager, List<String> titles) {
        return initNormalTab(mContext, false, viewpager, titles);
    }

    public CommonNavigator initNormalTab(Context mContext, boolean adjustMode, ViewPager viewpager, List<String> titles) {
        CommonNavigator navigator = new CommonNavigator(mContext);
        navigator.setAdjustMode(adjustMode);
        navigator.setScrollPivotX(0.75f);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles.get(index));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
                simplePagerTitleView.setNormalColor(Color.parseColor("#8D8D99"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#24272E"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewpager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(DisplayUtil.dip2px(context, 4));
                indicator.setLineWidth(DisplayUtil.dip2px(context, 20));
                indicator.setRoundRadius(DisplayUtil.dip2px(context, 3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(Color.parseColor("#fc5a44"));
                return indicator;
            }
        });
        return navigator;
    }
}
